package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesWorkSection;
import mesweb.mesweb.service.IMesWorkSectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@Api(tags="工段")
@RestController
@RequestMapping("/mes/mesworksection")
public class MesWorkSectionController {
    @Autowired
    private IMesWorkSectionService mESWorkSectionService;

    @GetMapping("/GetAll")
    @ApiOperation("获取动素类型列表")
    public CommonResult<?> GetAllActionTherbligsTypes(@ApiParam("动素类型名称") @RequestParam( value = "name",required = false) String name) {
        List<MesWorkSection> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mESWorkSectionService.list();
        } else {
            QueryWrapper<MesWorkSection> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("Work_Section_Name", name);
            data = mESWorkSectionService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }

    /**
     * 分页列表查询
     *
     * @param mESWorkSection
     * @param page
     * @param size
     * @param req
     * @return
     */
    @ApiOperation(value="工段-分页列表查询", notes="工段-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesWorkSection mESWorkSection,
                                         @RequestParam(name="page", defaultValue="1") Integer page,
                                         @RequestParam(name="size", defaultValue="10") Integer size,
                                         HttpServletRequest req) {
        QueryWrapper<MesWorkSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        Page<MesWorkSection> mesWorkSectionPage = new Page<MesWorkSection>(page, size);
        IPage<MesWorkSection> pageList = mESWorkSectionService.page(mesWorkSectionPage, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 添加
     *
     * @param mESWorkSection
     * @return
     */
    @ApiOperation(value="工段-添加", notes="工段-添加")
    @PostMapping(value = "/save")
    public CommonResult<?> add(@RequestBody MesWorkSection mESWorkSection) {
        boolean result = false;
        if(mESWorkSection.id == 0L) {
            QueryWrapper<MesWorkSection> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("work_section_no", mESWorkSection.getWorkSectionNo());
            queryWrapper.or().eq("work_section_name", mESWorkSection.getWorkSectionName());
            MesWorkSection mesTechnologyFlow1 = mESWorkSectionService.getOne(queryWrapper);
            if (mesTechnologyFlow1 != null) {
                String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getWorkSectionNo(), mesTechnologyFlow1.getWorkSectionName());
                throw new ApiException(msg);
            }
            result =  mESWorkSectionService.save(mESWorkSection);
        }else{
            result = mESWorkSectionService.updateById(mESWorkSection);
        }
        return CommonResult.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param mESWorkSection
     * @return
     */
    @ApiOperation(value="工段-编辑", notes="工段-编辑")
    @PutMapping(value = "/update")
    public CommonResult<?> edit(@RequestBody MesWorkSection mESWorkSection) {

        boolean result = false;
        if(mESWorkSection.id == 0L) {
            QueryWrapper<MesWorkSection> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("work_section_no",mESWorkSection.getWorkSectionNo());
            queryWrapper.or().eq("work_section_name",mESWorkSection.getWorkSectionName());
            MesWorkSection mesTechnologyFlow1 = mESWorkSectionService.getOne(queryWrapper);
            if(mesTechnologyFlow1 != null){
                String msg = StrUtil.format("编号{}或名称{}已经存在！",mesTechnologyFlow1.getWorkSectionNo(),mesTechnologyFlow1.getWorkSectionName());
                throw new ApiException(msg);
            }
            result = mESWorkSectionService.save(mESWorkSection);
        }
        else
        {
            result = mESWorkSectionService.updateById(mESWorkSection);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value="工段-通过id删除", notes="工段-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
        mESWorkSectionService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value="工段-批量删除", notes="工段-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("工段IDs") @RequestBody List<Long>  ids) {
        this.mESWorkSectionService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value="工段-通过id查询", notes="工段-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
        MesWorkSection mESWorkSection = mESWorkSectionService.getById(id);
        return CommonResult.success(mESWorkSection);
    }
}
